package com.huawei.spider.cnki.util;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import java.util.List;

/**
 * @Author: liminghui
 * @Date: 2020/5/23 23:04
 * @Version:
 * @Description:
 */
public class ExcelUtil {

    /**
     * 创建表头
     *
     * @param headers
     * @param sheet
     */
    public static void createHeader(String[] headers, HSSFSheet sheet) {
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString textString = new HSSFRichTextString(headers[i]);
            cell.setCellValue(textString);
            sheet.autoSizeColumn((short) i);
        }
    }

    /**
     * 创建表体
     * @param dataList
     * @param sheet
     */
    public static void createBody(List<List<String>> dataList, HSSFSheet sheet) {
        for (int a = 0; a < dataList.size(); a++) {
            HSSFRow row = sheet.createRow(a + 1);
            for (int j = 0; j < dataList.get(a).size(); j++) {
                HSSFCell cell = row.createCell(j);
                HSSFRichTextString textString = new HSSFRichTextString(dataList.get(a).get(j));
                cell.setCellValue(textString);
            }
        }
    }
}
